<%-- 
    Document   : doreserveticket
    Created on : Dec 18, 2012, 9:07:49 PM
    Author     : alexxxey
--%>

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Booking Tickets Web Appliaction</title>
    </head>
    <body>
        <h3>Резервирование билетов</h3>             
        <h4>Маршрут</h4> 
        
        <%@page import="javax.naming.*, bookingtickets.*" %>        
        <%!
            BookingTicketsSessionRemote ejbRef = null;            
                     
        %>        
        <%
            Trip trip = null;   
            
            InitialContext ic = new InitialContext();
            ejbRef = (BookingTicketsSessionRemote)ic.lookup("bookingtickets.BookingTicketsSessionRemote");                         
            String tripIdStr = new String(request.getParameter("chooseTrip"));
            trip = ejbRef.findTripById(new Integer(tripIdStr));
        %>
        
        <form action="doReserveTickets.jsp" method="POST">
            <table BORDER=2>
                <tr>
                    <td> </td>
                    <td align=center>Номер</td>                
                    <td align=center>Отправление</td>
                    <td align=center>Число мест</td>
                    <td align=center>Свободные места</td>                                    
                    <td>Пункты назначения</td> 
                </tr>

                <%
                    String tripId = new Integer(trip.getId()).toString();                                    
                    SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
                    SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");                      
                    String date = df.format(trip.getTripdate()) + " " + tf.format(trip.getTriptime()); 
                    String placesNum = new Integer(trip.getPlacesnum()).toString(); 
                    String freePlaces = new Integer(trip.getFreePlacesNum()).toString();
                    List destList = trip.getDestinationList();
                    String destStr = new String();
                    for (int i=0; i<destList.size(); ++i)
                    {
                        Destination d = (Destination)destList.get(i);
                        destStr += d.getDest();
                        if (i!=destList.size()-1)
                            destStr += ", ";
                    }

                %>
                <tr>
                    <td> <input type="radio" name="trip" checked
                                value=<%=(new Integer(trip.getId()).toString())%>> </td>
                    <td align=center> <%=(tripId)%> </td>
                    <td align=center> <%=(date)%> </td>
                    <td align=center> <%=(placesNum)%> </td>  
                    <td align=center> <%=(freePlaces)%> </td>                    
                    <td> <%=(" " + destStr)%> </td> 
                </tr>
            </table>   

            <h4>Свободные места</h4>        
            <table>
            <% 
                List<Integer> places = trip.getFreePlacesList();
                boolean rowStart = true;
                boolean rowEnd = false;                
                for (int i=1; i<=places.size(); ++i)
                {                
                    if (rowStart)
                    {
                    %>
                        <tr>
                    <%                    
                        rowStart = false;
                        rowEnd = true;
                    }

                    if (i==1)
                    {
                    %>
                        <td> 
                            <input type="checkbox" name="places" checked
                                   value=<%=(places.get(i-1).toString())%>>
                        </td>
                        <td> <%=(places.get(i-1).toString())%> </td>                    
                    <%                       
                    }
                    else
                    {
                    %>
                        <td>                        
                            <input type="checkbox" name="places"
                                   value=<%=(places.get(i-1).toString())%>>
                        </td>
                        <td> <%=(places.get(i-1).toString())%> </td>                    
                    <%
                    }  

                    if (i%10==0 && i!=1 && rowEnd)
                    {
                    %>
                        </tr>
                    <%
                        rowEnd = false;
                        rowStart = true;
                    }                                             
                }
            %>
            </table>

            <h4>Личные данные</h4>
            <table>            
                <tr>
                    <td>Имя</td>
                    <td> <input type="text" name="txtFirstName"> </td>
                </tr>
                
                <tr>
                    <td>Фамилия</td>
                    <td> <input type="text" name="txtLastName"> </td>
                </tr>
                
                <tr>
                    <td>Номер паспорта</td>
                    <td> <input type="text" name="txtPasport"> </td>
                </tr>
                
            </table>
            
             <input type="submit" value="Зарезервировать" />
        </form>       
        
            <p><a href="findTrip.jsp">Поиск маршрутов</a></p>
        <p><a href="index.jsp">Главная страница</a></p>
    </body>
</html>
